﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LectorHuellaC
{
    public partial class FormCategoriaProductos : Form
    {
        log4net.ILog logger;
        cls_category _cls_category = new cls_category();
        public FormCategoriaProductos()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = log4net.LogManager.GetLogger(typeof(Program));
            InitializeComponent();
            getAllCategoryes();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {


                if (txtnombre.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Campo precio venta no puede estar vacion", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                
                int category_id = 0;
                if (!chkcategory.Checked)
                {
                    category_id = Convert.ToInt32(PrintCellRowAndCol());

                }

                string nombre_category = txtnombre.Text;
                string descripcion = txtdescripcion.Text;



                bool resp = _cls_category.insertarActualizarCategory(category_id,nombre_category,descripcion);
                if (resp)
                {
                    MessageBox.Show("Se realizo la insercion", "Insercion productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    getAllCategoryes();
                   
                }
                else
                {
                    MessageBox.Show("Se ha presentado un problema", "Insercion productos", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    getAllCategoryes();
                }
            }
            catch (Exception ex)
            {
                logger.Error("Error" + ex.Message);
            }
        }

        public void getAllCategoryes(){
            dataGridView1.DataSource = _cls_category.getCategories();
        }

        public int PrintCellRowAndCol()
        {
            // Prints the value of the cell through the DataTable.
            DataTable myTable;
            // Assumes the DataGrid is bound to a DataTable.
            myTable = (DataTable)dataGridView1.DataSource;
            int category_id = 0;
            if (dataGridView1.Rows.Count != 0)
            {
                DataGridViewCell myCell;
                myCell = dataGridView1.CurrentCell;
                if (myCell != null)
                {
                    if (myCell.Selected)
                    {
                        DataRow values = myTable.Rows[myCell.RowIndex];
                        category_id = Convert.ToInt32(values[0]);
                    }
                }
            }
            return category_id;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            DataGridViewRow row = dataGridView1.CurrentRow;

            if (!string.IsNullOrEmpty(Convert.ToString(row.Cells["category_id"].Value)))
            {

                DataTable myTable;
                myTable = (DataTable)dataGridView1.DataSource;

                DataGridViewCell myCell;
                myCell = dataGridView1.CurrentCell;


                DataRow values = myTable.Rows[myCell.RowIndex];

                txtnombre.Text = Convert.ToString(values["category_name"]);
                txtdescripcion.Text = Convert.ToString(values["category_description"]);
            }
            else
            {
                //limpiarControles();
            }

        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            string search = "";
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                search = txtsearch.Text;
                dataGridView1.DataSource = _cls_category.getCategoriesbyCriteria(search);
            }
        }
    }
}
